package org.colcon.web.client.ui.deprecated;

import org.colcon.web.client.Collective_Congress;
import org.colcon.web.client.ui.ColConPanel;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class CreateGroupPanel extends ColConPanel
{
	private VerticalPanel panel = new VerticalPanel();
	
	private Grid grid = new Grid(3, 2);
	
	private Label nameLabel = new Label("Name:");
	private TextBox nameBox = new TextBox();
	private Label charterLabel = new Label("Charter:");
	private TextArea charterBox = new TextArea();
	private Button button = new Button("Create Group");
	
	public CreateGroupPanel(Collective_Congress client)
	{
		super(client, null, null);
		
		setStyleName("register");
		
		panel.setWidth("100%");
		panel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
		
		button.addClickHandler(new MyButtonHandler());
		
		sinkEvents(Event.ONKEYPRESS);
		addHandler(new MyEnterHandler(), KeyPressEvent.getType());
		
		grid.setWidget(0, 0, nameLabel);
		grid.setWidget(0, 1, nameBox);
		grid.setWidget(1, 0, charterLabel);
		grid.setWidget(1, 1, charterBox);
		grid.setWidget(2, 1, button);

		panel.add(grid);
		add(panel);
		
		setFocus(nameBox);
	}
	
	private class MyButtonHandler implements ClickHandler
	{
		public void onClick(ClickEvent event) 
		{
			buttonClick();
		}
	}
	
	private class MyEnterHandler implements KeyPressHandler
	{
		public void onKeyPress(KeyPressEvent event) 
		{
			if(event.getCharCode() == KeyCodes.KEY_ENTER)
				buttonClick();
		}
	}
	
	public void buttonClick() 
	{
		if(!validate())
		{
			panel.clear();
			panel.add(grid);
			panel.add(new HTML("Invalid input"));
			return;
		}
		
		panel.clear();
		panel.add(new HTML("Loading..."));
		
		getService().addGroup(nameBox.getText(), charterBox.getText(), new AsyncCallback<Boolean>()
		{
			public void onFailure(Throwable caught)
			{
				caught.printStackTrace();
				
				panel.clear();
				panel.add(grid);
				panel.add(new HTML(caught.getMessage()));
			}
			
			public void onSuccess(Boolean result)
			{
				panel.clear();
				
				if(result)
				{
					panel.add(new HTML("Group creation successful"));
				}
				else
				{
					panel.add(grid);
					panel.add(new HTML("Name already taken"));
				}
			}
		});
	}
	
	private boolean validate()
	{
		return
			!(
				nameBox.getText().isEmpty() ||	
				charterBox.getText().isEmpty()
			);
	}
}
